﻿using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Windows.Forms;
using LuaFramework;
using System;
using System.Collections.Generic;

enum typeEnum{
	prefab,
	png,
	jpg
}
public class AutoWriteCsv : EditorWindow
{
	List<string> strList = new List<string>();
	string[] remarkList = {"包名:","文件类型:","地址:" };
	List<typeEnum> typeSelect = new List<typeEnum>();
	// Use this for initialization
	void Start ()
	{
	 
	}
	
	// Update is called once per frame
	void Update ()
	{
		
	}

	void OnGUI(){
		GUILayout.Label("csv自动填写面板", EditorStyles.boldLabel);
		if (GUI.Button (new Rect(0,0,160,30),"加载Csv文件")) {
			loadCsvFile ();
		}

		if (GUI.Button (new Rect(200,0,160,30),"保存文件")) {
			saveFile ();
		}
		GUILayout.Label ("");
		GUILayout.Label ("——————————————————————————————————————————————————————————————————————————————————————————————————————————————————");
		if (strList.Count > 0) {
			for (int i = 0; i < strList.Count; i++) {
				string[] sconent = strList [i].Split (',');
				for (int a = 0; a < sconent.Length; a++) {
					GUI.Label (new Rect(a*210,i*22+60,60,20),remarkList[a]);
					if (a == 0) {
						GUI.TextField (new Rect (60, i * 22 + 60, 100, 20), sconent [a]);
					} else if (a == 1) {
						typeSelect[i] = (typeEnum)EditorGUI.EnumPopup (new Rect (280, i * 22 + 60, 80, 20),typeSelect[i]);
					} else if (a == 2) {
						GUI.TextField (new Rect (500, i * 22 + 60, 300, 20), sconent [a]);
					}
				}
				if (GUI.Button (new Rect (900, i * 22 + 60, 100, 20), "修改")) {
					updateItem (i);
				}
				if (GUI.Button (new Rect (800, i * 22 + 60, 100, 20), "删除")) {
					deleteItem (i);
				}
			}
			GUI.Label (new Rect(0,strList.Count*22+80,1000,20),"——————————————————————————————————————————————————————————————————————————————————————————————————————————————————");
		}

		if (GUI.Button (new Rect (0, strList.Count * 22 + 100, 100, 20), "自动添加")) {
			addItem ();
		}
	}

	void addItem(){
		string temp = selectString ();
		if (strList == null) {
			strList = new List<string> ();
			typeSelect = new List<typeEnum> ();
		}
		strList.Add (temp);
		typeSelect.Add (typeEnum.prefab);
	}
	void deleteItem(int idx){
		strList.RemoveAt (idx);
		typeSelect.RemoveAt (idx);
	}

	void updateItem(int idx){
		string temp = selectString ();
		strList [idx] = temp;
		typeSelect [idx] = typeEnum.prefab;
	}
	//得到当前选中的文件信息
	string selectString(){
		string result = "";
		UnityEngine.Object[] arr=Selection.GetFiltered(typeof(UnityEngine.Object), UnityEditor.SelectionMode.TopLevel);  
		if (arr.Length > 0) {
			string packageName = arr[0].ToString().Substring(0,arr[0].ToString().IndexOf(" "));
			result = packageName + AppConst.ExtName + ","+getTypeString(typeEnum.prefab)+"," + AssetDatabase.GetAssetPath (arr [0]);
		}
		return result;
	}

	string getTypeString(typeEnum te){
		string result = "*.prefab";
		switch (te) {
			case typeEnum.prefab:
			break;
		case typeEnum.png:
			result = "*.png";
			break;
		case typeEnum.jpg:
			result = "*.jpg";
			break;
		}
		return result;
	}

	typeEnum getTypeEnum(string value){
		typeEnum result = typeEnum.prefab;
		switch (value) {
		case "*.prefab":
			break;
		case "*.png":
			result = typeEnum.png;
			break;
		case "*.jpg":
			result = typeEnum.jpg;
			break;
		}
		return result;
	}

	void loadCsvFile(){
		strList = null;
		string path = EditorUtility.OpenFilePanel ("选择一个CSV文件",AppConst.csv_path,"csv");
		StreamReader sr = null;
		try{
			sr = File.OpenText(path);
			strList = new List<string>();
			typeSelect = new List<typeEnum>();
		}catch(Exception e){
			MyDebug.LogRed ("没有找到对应的文件 => "+path);
			return;
		}

		string line;
		while((line = sr.ReadLine()) != null){
			strList.Add (line);
			string[] temps = line.Split (',');
			typeSelect.Add (getTypeEnum(temps[1]));
		}
		sr.Close ();
		sr.Dispose ();
	}

	void saveFile(){
		if (strList.Count　> 0) {
			string path = EditorUtility.SaveFilePanel ("保存csv文件", AppConst.csv_path, "", "csv");
			string writeContent = "";
			for (int i = 0; i < strList.Count; i++) {
				string[] temps = strList [i].Split (',');
				writeContent += temps[0]+","+getTypeString(typeSelect[i])+","+temps[2]+"\n";
			}
			try {
				File.WriteAllText (path,writeContent);
			} catch (Exception e) {
				return;
			}
		}
	}
}

